import { IChannel } from '@/store/reducer/channel';

export interface AuthType {
  refresh_token: string;
  token: string;
}
const AUTH_KEY = 'geek-park-78';
export const saveAuth = (auth: AuthType) => {
  localStorage.setItem(AUTH_KEY, JSON.stringify(auth));
};

export const getAuth = (): AuthType => {
  return JSON.parse(localStorage.getItem(AUTH_KEY) || '{}');
};

export const removeAuth = () => {
  localStorage.removeItem(AUTH_KEY);
};

export const hasToken = (): boolean => {
  return !!getAuth().token;
};

const CHANNEL_KEY = 'itcast-geek-park-channel';

/**
 * 保存频道数据到本地
 * @param {*} channels
 */
export const setLocalChannels = (channels: IChannel[]) => {
  localStorage.setItem(CHANNEL_KEY, JSON.stringify(channels));
};

/**
 * 获取本地的频道数据，如果没有数据，使用时注意空数组的判断
 * @returns
 */
export const getLocalChannels = (): IChannel[] => {
  return JSON.parse(localStorage.getItem(CHANNEL_KEY) || '[]');
};

/**
 * 删除本地的频道数据
 */
export const removeLocalChannels = () => {
  localStorage.removeItem(CHANNEL_KEY);
};
